<?php
/**
 * 发圈任务接取列表
 * author itholiday@126.com
 */
global $_W, $_GPC;
$html_title = $_W['current_module']['title'];

load()->func('tpl');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';
//页面显示条数
$pagesize = 15;
$page = max(1, $_GPC['page']);
$start = ($page - 1) * $pagesize;

$where = 'A.id>0 and A.review_status=0 ';

$userinfo = isset($_GPC['userinfo']) && !empty($_GPC['userinfo']) ? input('userinfo') : '';
$start_time_2 = isset($_GPC['start_time_2']) && !empty($_GPC['start_time_2']) ? strtotime($_GPC['start_time_2']) : '';
$end_time_2 = isset($_GPC['end_time_2']) && !empty($_GPC['end_time_2']) ? strtotime($_GPC['end_time_2']) : '';
if (!empty($userinfo)) {
    $where .= " and (B.mobile like '%$userinfo%' or B.truename like '%$userinfo%') ";
}

if (!empty($start_time_2) && !empty($end_time_2)) {
    $where .= " and A.create_time>=$start_time_2 and A.create_time<=$end_time_2 ";
} else {
    if (!empty($start_time_2)) {
        $where .= " and A.create_time>=$start_time_2 ";
    }
    if (!empty($end_time_2)) {
        $where .= " and A.create_time<=$end_time_2 ";
    }
}

function reviewTask($receive, $yes = 0, $notes = '')
{
    // 对应的任务信息
    $userinfo = pdo_get('yq_haircircle_users', array('id' => $receive['user_id']));
    if (empty($userinfo) || $userinfo['user_status'] != 1) {
        ajaxError('用户ID' . $receive['user_id'] . '不存在或被禁用,该用户的任务订单不可审核！');
    }

    if ($userinfo['user_level'] < 1) {
        ajaxError('该用户不是会员，不能进行发圈任务');
    }

    $share_setting = cmf_get_option('share_setting');
    $task_money = isset($share_setting['task_money']) ? $share_setting['task_money'] : 0;
    if ($task_money <= 0) {
        ajaxError('发圈任务暂无设置奖励，无需审核');
    }

    pdo_begin();

    try {
        if ($yes == 1) {
            // 申请通过
            // 修改用户余额
            $res = pdo_update('yq_haircircle_users', array('user_money' => $userinfo['user_money'] + $task_money), array('id' => $receive['user_id']));
            if (empty($res)) {
                pdo_rollback();
                ajaxError('修改用户余额失败');
            }

            // 加入会员流水日志
            $scoreLog = [
                'oid' => $receive['id'],
                'user_id' => $receive['user_id'],
                'mobile' => $userinfo['mobile'],
                'change' => $task_money,
                'balance' => $task_money + $userinfo['user_money'],
                'type' => 1,//完成任务
                'create_time' => time(),
                'remark' => "完成发圈任务,奖励金额:" . $task_money,
            ];
            $scoreLogIns = pdo_insert('yq_haircircle_user_money_log', $scoreLog);
            if (empty($scoreLogIns)) {
                pdo_rollback();
                ajaxError('添加用户资金日志失败');
            }

            $data['review_status'] = 1;//通过
            $data['review_mark'] = '';
            $data['handle_id'] = $_W['uid'];
            $data['handle_time'] = time();

            // 修改任务状态
            $receiveUp = pdo_update('yq_haircircle_share_task_log', $data, array('id' => $receive['id']));
            if (empty($receiveUp)) {
                pdo_rollback();
                ajaxError('修改审核状态失败');
            }

            pdo_commit();

            return true;
        } else {

            // 申请驳回
            if (empty($notes)) {
                ajaxError('请填写驳回原因');
            }


            //任务驳回消息通知
            $news = [
                'user_id' => $receive['user_id'],
                'oid' => $receive['id'],
                'title' => "发圈任务驳回通知",
                'create_time' => time(),
                'type' => 1,
                'status' => 0,
                'content' => "发圈任务已驳回！驳回原因：" . $notes
            ];
            $newsIns = pdo_insert('yq_haircircle_user_message', $news);
            if (empty($newsIns)) {
                pdo_rollback();
                ajaxError('添加用户通知失败');
            }

            $data['review_status'] = -1;//驳回
            $data['review_mark'] = $notes;
            $data['handle_id'] = $_W['uid'];
            $data['handle_time'] = time();

            // 修改任务状态
            $receiveUp = pdo_update('yq_haircircle_share_task_log', $data, array('id' => $receive['id']));
            if (empty($receiveUp)) {
                pdo_rollback();
                ajaxError('修改审核状态失败');
            }

            pdo_commit();

            return true;
        }
    } catch (\Exception $e) {
        pdo_rollback();
        ajaxError('系统出现异常:' . $e->getMessage());
    }
}

if ($op == 'review') {
    //单条审核
    if (isset($_GPC['id'])) {
        $id = intval($_GPC['id']);
        $yes = $_GPC['yes'] ? 1 : 0;
        $review_mark = isset($_GPC['review_mark']) ? trim($_GPC['review_mark']) : '';
        $receive_order = pdo_get('yq_haircircle_share_task_log', array('id' => $id));
        //判断审核状态
        if ($receive_order['receive_status'] != 0) {
            ajaxError('该订单已处理过');
        }
        reviewTask($receive_order, $yes, $review_mark);
        ajaxSuccess('审核完成');
    }
    //批量审核
    if (isset($_GPC['ids'])) {
        $ids = $_GPC['ids'];
        $yes = $_GPC['yes'] ? 1 : 0;
        foreach ($ids as $id) {
            $review_mark = isset($_GPC['review_mark']) ? trim($_GPC['review_mark']) : '';
            $receive_order = pdo_get('yq_haircircle_share_task_log', array('id' => $id));
            //判断审核状态
            if ($receive_order['receive_status'] != 0) {
                ajaxError('该订单已处理过');
            }
            reviewTask($receive_order, $yes, $review_mark);
        }
        ajaxSuccess('审核完成');
    }
}
if ($op == 'list' || $op == '') {

    $total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' . tablename('yq_haircircle_share_task_log') . ' A left join ims_yq_haircircle_users B on A.user_id=B.id WHERE ' . $where);

    //生成分页HTML
    $pager = getPageHTML($total, $page, $pagesize);
    $sql = "select A.*,B.mobile from ims_yq_haircircle_share_task_log A left join ims_yq_haircircle_users B on A.user_id=B.id where $where order by A.id desc limit $start,$pagesize";
    $list = pdo_fetchall($sql);
    $list = $list ? $list : array();
    if ($list) {
        foreach ($list as $k => $v) {
            if (!empty($v['submit_img'])) {
                $thumbs = explode(';', trim($v['submit_img'], ';'));
                foreach ($thumbs as $kk => $vv) {
                    $thumbs[$kk] = cmf_get_upload_url($vv);
                }
                $list[$k]['submit_img'] = implode(';', $thumbs);
            } else {
                $list[$k]['submit_img'] = '';
            }

        }
    }
    include $this->template('web/share_task_log_review');
    exit;
}